import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JRadioButtonMenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * GPanelPopup is the popup menu for the panel.
 *
 * @version 1.0
 * @author P.Jaya Krishna
 */

class GPanelPopup extends JPopupMenu implements ActionListener
{
	private GPanel gPanel;
	private JCheckBoxMenuItem showWeights;
	private JCheckBoxMenuItem stepByStep;
	private JRadioButtonMenuItem selection;
	private JRadioButtonMenuItem vertex;
	private JRadioButtonMenuItem edge;
	private JMenuItem adjMatrix;
	private JMenuItem undo;
	private JMenuItem redo;
	private JMenuItem refresh;
	
	/**
	 * Creates a popmenu for the GPanel at the given location (x,y)
	 */
	public GPanelPopup(GPanel gPanel, int x, int y)
	{
		this.gPanel = gPanel;
		
		undo			= new JMenuItem("Undo");
		redo			= new JMenuItem("Redo");
		refresh			= new JMenuItem("Refresh");
		showWeights 	= new JCheckBoxMenuItem("Show Weights");
		adjMatrix 		= new JMenuItem("Adjacency Matrix");
		stepByStep		= new JCheckBoxMenuItem("Graph Animation");
		JMenu operation = new JMenu("Operation");
		selection 		= new JRadioButtonMenuItem("Selection Mode");
		vertex 			= new JRadioButtonMenuItem("Vertex Mode");
		edge 			= new JRadioButtonMenuItem("Edge Mode");
		
		showWeights.setMnemonic('W');
		adjMatrix.setMnemonic('A');
		undo.setMnemonic('U');
		redo.setMnemonic('R');
		refresh.setMnemonic('f');
		stepByStep.setMnemonic('B');
		selection.setMnemonic('S');
		vertex.setMnemonic('V');
		edge.setMnemonic('E');
		
		
		if(gPanel.getCurrentAction() == gPanel.ACTION_SELECT)
			selection.setSelected(true);
		else if(gPanel.getCurrentAction() == gPanel.ACTION_VERTEX)
				vertex.setSelected(true);
		else if(gPanel.getCurrentAction() == gPanel.ACTION_EDGE)
				edge.setSelected(true);
				
		if(gPanel.getShowEdgeWeights())
			showWeights.setSelected(true);
			
		if(gPanel.animationDelay != 0)
			stepByStep.setSelected(true);
			
		undo.setEnabled(gPanel.gFrame.toolBar.TBUndo.isEnabled());
		redo.setEnabled(gPanel.gFrame.toolBar.TBRedo.isEnabled());
		
		add(undo);
		add(redo);
		addSeparator();
		add(refresh);
		add(adjMatrix);
		addSeparator();
		add(stepByStep);
		add(showWeights);
		addSeparator();
		add(operation);
		operation.add(selection);
		operation.add(vertex);
		operation.add(edge);
		
		undo.addActionListener(this);
		redo.addActionListener(this);
		refresh.addActionListener(this);
		showWeights.addActionListener(this);
		adjMatrix.addActionListener(this);
		stepByStep.addActionListener(this);
		selection.addActionListener(this);
		vertex.addActionListener(this);
		edge.addActionListener(this);
		
		setLocation(x,y);
		show(gPanel, x, y);
	}
	
	public void actionPerformed(ActionEvent ae)
	{
		String s = (String) ae.getActionCommand();
		
		if(s.equals("Show Weights"))
		{
			gPanel.gFrame.toolBar.TBShowWeights.doClick();
		}
		else if(s.equals("Adjacency Matrix"))
		{
			gPanel.gFrame.toolBar.TBAdjMatrix.doClick();
		}
		else if(s.equals("Graph Animation"))
		{
			gPanel.gFrame.toolBar.TBStepByStep.doClick();
		}
		else if(s.equals("Selection Mode"))
		{
			gPanel.gFrame.toolBar.TBSelect.doClick();
		}
		else if(s.equals("Vertex Mode"))
		{
			gPanel.gFrame.toolBar.TBVertex.doClick();
		}
		else if(s.equals("Edge Mode"))
		{
			gPanel.gFrame.toolBar.TBEdge.doClick();
		}
		else if(s.equals("Undo"))
		{
			gPanel.gFrame.toolBar.TBUndo.doClick();
		}
		else if(s.equals("Redo"))
		{
			gPanel.gFrame.toolBar.TBRedo.doClick();
		}
		else if(s.equals("Refresh"))
		{
			gPanel.gFrame.toolBar.TBRefresh.doClick();
		}
	}
}